﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.EHR.Library;
using ZX.Common;
using ZX.Common.Extend;
using ZX.DBModule;

//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：it012
// Email: 
// 日 期：2012/3/28
// 描 述：员工基本信息业务处理
// 版 本：1.0.0.0    
//    
//==================================================================

namespace ZX.BLL
{
    public class BEmployeeAffairs : BaseBussnies
    {

        public override bool Insert(object entitys, out string msg)
        {
            bool rs = base.Insert(entitys, out msg);
            if (rs)
            {
                RunEmployeeJob();
            }
            return rs;
        }

        public override bool Update(object entitys, out string msg)
        {
            bool rs = base.Update(entitys, out msg);
            if (rs)
            {
                RunEmployeeJob();
            }
            return rs;
        }

        private void RunEmployeeJob()
        {
            try
            {
                //人事事务处理
                BaseEntity instance = null;
                ZX.BLL.Extend.BaseBussnies bll = null;
                IList<BaseEntity> list = null;
                BaseEntity hrEmployeeInstance = ZX.Common.Globals.getEntity("hrEmployee") as BaseEntity;
                ZX.BLL.Extend.BaseBussnies hrEmployeeBll = new ZX.BLL.Extend.BaseBussnies(hrEmployeeInstance);
                instance = ZX.Common.Globals.getEntity("hrEmployeeAffairs") as BaseEntity;
                bll = new ZX.BLL.Extend.BaseBussnies(instance);
                string condition = string.Format(" EffectiveDate<='{0}' and flag=0", DateTime.Now.ToString("yyyy-MM-dd"));//条件为到达生效日期,并且异动类型为离职
                list = bll.GetEntitysByCondition(condition);//获取所有符合条件的记录
                if (list != null)
                {
                    foreach (BaseEntity entity in list)
                    {
                        hrEmployeeAffairs hrEmpAffairs = entity as hrEmployeeAffairs;
                        byte empStatus = 1;//员工状态(1.在职；2.不在职，3.离职，4.离退休)
                        bool flag = false;//更新是否停用账户
                        //事务类型（1.在职转不在职，2.不在职转在职,3.辞职,4.辞退,5.离退休,6.返聘,7.解返聘,8.转正）
                        switch (hrEmpAffairs.AffairsType) //人事事务
                        {
                            case 1://在职转不在职
                                empStatus = 2;
                                flag = true;
                                break;
                            case 2://不在职转在职
                            case 6://返聘
                                empStatus = 1;
                                flag = false;
                                break;
                            case 3:
                            case 4:
                                empStatus = 3;
                                flag = false;
                                break;
                            case 5:
                            case 7:
                                empStatus = 4;
                                flag = false;
                                break;
                            case 8:
                                empStatus = 254;//转正
                                break;
                        }
                        hrEmployee hremployee = hrEmployeeBll.GetEntityById(hrEmpAffairs.EmployeeID.ToString()) as hrEmployee;
                        if (empStatus == 254)//转正
                        {
                            hremployee.EmployeeType = new Guid("D924C147-1A23-4FBE-A958-2F58FD9377D7");
                        }
                        else
                        {
                            hremployee.EmployeeStatus = empStatus;//修改员工状态
                        }
                        hrEmployeeBll.Update(hremployee);
                        try
                        {
                            hrEmpAffairs.flag = 1;
                            bll.Update(hrEmpAffairs);
                        }
                        catch { }
                        if (empStatus == 3)//离职后用户账户停用
                        {
                            //try
                            //{
                            //    //更新用户表
                            //    condition = string.Format("EmpID='{0}'", hrEmpAffairs.EmployeeID.ToString());
                            //    instance = ZX.Common.Globals.getEntity("DatUser") as BaseEntity;
                            //    ZX.BLL.Extend.BaseBussnies DatUserBll = new ZX.BLL.Extend.BaseBussnies(instance);
                            //    DatUser datUser = DatUserBll.GetEntityByCondition(condition) as DatUser;//获取该员工用户信息
                            //    datUser.IsExpire = true;
                            //    datUser.ExpireDate = DateTime.Now;
                            //    datUser.AddUser = Guid.Empty;
                            //    DatUserBll.Update(datUser);//更新员工用户表
                            //}
                            //catch
                            //{ }
                        }
                    }
                }
            }
            catch
            { }
        }
    }
}
